package com.irdstudio.efp.esb.api.rest.sx;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.efp.esb.api.bean.sx.ReqCreditBean;
import com.irdstudio.efp.esb.common.constant.ESBServerConstance;
import com.irdstudio.efp.esb.common.constant.nls.NlsProdCdEnum;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;

/**
 * 网贷授信进件申请
  * @author heyingfeng
  * @since 2019年3月4日 下午2:52:38
  * @version 1.0
 */
@Controller
@RequestMapping("/esb")
public class NlsCreditController {
	private static Logger logger = LoggerFactory.getLogger(NlsCreditController.class);

	/**
	 * 根据渠道，产品代码分发场景class，主要用于分辨获取字段注解的校验
	 * @author heyingfeng
	 * @since 2019年3月14日 下午8:20:07
	 * @date 2019年3月14日 下午8:20:07
	 * @version 0.1 初始为0.1,后续变更则修改
	 */
	@PostMapping("/SERVICE_30220001_03")
	public String doRecv(HttpServletRequest request) throws Exception {
		logger.info("校验 ESB请求体对象！");
		EsbReqServiceBean esbReqServiceBean = (EsbReqServiceBean) request.getAttribute("esbReqServiceBean");
		String esbReqBody = (String) request.getAttribute("esbReqBody");
		ReqCreditBean reqLoanAppBean = JSONObject.parseObject(esbReqBody, ReqCreditBean.class);
		String svcCd = esbReqServiceBean.getSysHead().getSvcCd(); // 服务代码
		String scnCd = esbReqServiceBean.getSysHead().getScnCd(); // 场景
		String prdCode = reqLoanAppBean.getProdCd();
		String chanNo = reqLoanAppBean.getChanNo();
		String tradeCode = "";
		if(NlsProdCdEnum.MYD.getValue().equals(prdCode) || NlsProdCdEnum.ZXD.getValue().equals(prdCode)) {//百度
			tradeCode = ESBServerConstance.SERVICE_KEY_HEAD + svcCd + "_" + scnCd + "_" + chanNo;// 交易码
		}
		return "forward:/esb/" + tradeCode;
	}
}
